package com.example.ecs.demo;

import com.example.ecs.*;
import com.example.ecs.systems.RenderSystem;
import com.example.ecs.systems.physics.SATCollisionSystem;
import com.example.ecs.components.PositionComponent;
import com.example.ecs.components.VelocityComponent;
import com.example.ecs.components.physics.BoundaryComponent;
import com.example.ecs.components.physics.CircleCollisionComponent;
import com.example.ecs.components.physics.SATCollisionComponent;
import com.example.ecs.components.render.CircleRenderComponent;
import com.example.ecs.components.render.PolygonRenderComponent;
import com.example.ecs.components.render.RectangleRenderComponent;
import com.example.ecs.core.Entity;
import com.example.ecs.core.EntityManager;
import com.example.ecs.systems.MovementSystem;
import com.example.ecs.systems.physics.BoundarySystem;
import com.example.ecs.systems.physics.CircleCollisionSystem;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * GamePanel类是游戏的主面板，负责渲染和游戏循环
 */
public class GamePanel extends JPanel implements ActionListener {
    private EntityManager entityManager;
    private RenderSystem renderSystem;
    private MovementSystem movementSystem;
    private BoundarySystem boundarySystem;
    private SATCollisionSystem satCollisionSystem;
    private CircleCollisionSystem circleCollisionSystem;
    private Timer gameTimer;
    
    public GamePanel() {
        initGame();
        initGameTimer();
    }
    
    private void initGame() {
        entityManager = new EntityManager();
        
        // 创建系统
        renderSystem = new RenderSystem(null);
        movementSystem = new MovementSystem();
        boundarySystem = new BoundarySystem(getWidth(), getHeight());
        satCollisionSystem = new SATCollisionSystem();
        circleCollisionSystem = new CircleCollisionSystem();

        // 创建一些示例实体
        createSampleEntities();


        // 添加系统到实体管理器
        entityManager.addSystem(renderSystem);
        entityManager.addSystem(movementSystem);
        entityManager.addSystem(boundarySystem);
        entityManager.addSystem(satCollisionSystem);
        entityManager.addSystem(circleCollisionSystem);
        
        // 初始化实体管理器（确保系统能接收到实体）
        entityManager.initialize();

    }
    
    private void createSampleEntities() {
        // 创建一个移动的红色正方形（4边形）
        Entity entity1 = entityManager.createEntity();
        entity1.addComponent(new PositionComponent(100, 100));
        entity1.addComponent(new VelocityComponent(100, 50)); // 每秒移动100像素
        entity1.addComponent(new RectangleRenderComponent(Color.RED, 30, 30));
        // 创建正方形的顶点
        double[][] squareVertices = {{0, 0}, {30, 0}, {30, 30}, {0, 30}};
        entity1.addComponent(new SATCollisionComponent(squareVertices));
        entity1.addComponent(new BoundaryComponent(0, 0, getWidth() - 30, getHeight() - 30));
        
        // 创建一个移动的蓝色五边形
        Entity entity2 = entityManager.createEntity();
        entity2.addComponent(new PositionComponent(300, 200));
        entity2.addComponent(new VelocityComponent(-80, -60)); // 每秒移动
        // 创建五边形的顶点
        double[][] pentagonVertices = {
            {25, 0}, 
            {47.5, 15.5}, 
            {37.5, 45.5}, 
            {12.5, 45.5}, 
            {2.5, 15.5}
        };
        entity2.addComponent(new PolygonRenderComponent(Color.BLUE, pentagonVertices));
        entity2.addComponent(new SATCollisionComponent(pentagonVertices));
        entity2.addComponent(new BoundaryComponent(0, 0, getWidth() - 50, getHeight() - 50));
        
        // 创建一个移动的绿色八边形
        Entity entity3 = entityManager.createEntity();
        entity3.addComponent(new PositionComponent(200, 300));
        entity3.addComponent(new VelocityComponent(-50, -30)); // 每秒移动
        // 创建八边形的顶点
        double[][] octagonVertices = {
            {10, 0}, 
            {30, 0}, 
            {40, 10}, 
            {40, 30}, 
            {30, 40}, 
            {10, 40}, 
            {0, 30}, 
            {0, 10}
        };
        entity3.addComponent(new PolygonRenderComponent(Color.GREEN, octagonVertices));
        entity3.addComponent(new SATCollisionComponent(octagonVertices));
        entity3.addComponent(new BoundaryComponent(0, 0, getWidth() - 40, getHeight() - 40));
        
        // 创建一个移动的黄色圆形
        Entity entity4 = entityManager.createEntity();
        entity4.addComponent(new PositionComponent(400, 100));
        entity4.addComponent(new VelocityComponent(60, 70)); // 每秒移动
        entity4.addComponent(new CircleRenderComponent(Color.YELLOW, 15)); // 半径15
        entity4.addComponent(new CircleCollisionComponent(15)); // 半径15
        entity4.addComponent(new BoundaryComponent(15, 15, getWidth() - 15, getHeight() - 15));
    }
    
    private void initGameTimer() {
        gameTimer = new Timer(16, this); // 约60 FPS
        gameTimer.start();
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        
        Graphics2D g2d = (Graphics2D) g.create();
        
        // 设置抗锯齿渲染提示
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        
        // 更新渲染系统的Graphics对象
        renderSystem.setGraphics(g2d);
        
        // 渲染所有实体
        renderSystem.update(0); // 渲染不需要deltaTime
        
        g2d.dispose();
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        // 更新边界系统中的屏幕尺寸
        boundarySystem.setScreenSize(getWidth(), getHeight());
        
        // 为带有边界组件的实体更新边界
        for (Entity entity : entityManager.getEntities()) {
            if (entity.hasComponent(BoundaryComponent.class)) {
                BoundaryComponent boundary = entity.getComponent(BoundaryComponent.class);
                // 更新边界为当前屏幕大小减去实体尺寸
                if (entity.hasStrictComponent(RectangleRenderComponent.class)) {
                    RectangleRenderComponent render = entity.getComponent(RectangleRenderComponent.class);
                    boundary.maxX = getWidth() - render.width;
                    boundary.maxY = getHeight() - render.height;
                }
                // 对于圆形实体，使用半径更新边界
                else if (entity.hasStrictComponent(CircleRenderComponent.class)) {
                    CircleRenderComponent render = entity.getComponent(CircleRenderComponent.class);
                    boundary.minX = (int) render.radius;
                    boundary.minY = (int) render.radius;
                    boundary.maxX = getWidth() - (int) render.radius;
                    boundary.maxY = getHeight() - (int) render.radius;
                }
                // 对于多边形实体，使用近似边界
                else if (entity.hasStrictComponent(PolygonRenderComponent.class)) {
                    // 简单估计多边形的边界
                    boundary.maxX = getWidth() - 50;
                    boundary.maxY = getHeight() - 50;
                }
            }
        }
        
        // 计算deltaTime（以秒为单位）
        double deltaTime = 16.0 / 1000.0; // 16毫秒转换为秒
        
        // 更新所有系统
        entityManager.update(deltaTime);
        
        // 重绘面板
        repaint();
    }
}